const recast = require('recast');
const fs = require('fs');
const path = require('path');
const parser = require('@babel/parser');

// 定义一个使用@babel/parser的解析器配置
const useBabelParser = {
  parser: {
    parse(source) {
      return parser.parse(source, {
        plugins: ["typescript", "jsx"],
        sourceType: "module",
      });
    }
  }
};

// 读取TypeScript文件
const code = fs.readFileSync(path.resolve(__dirname, 'code2.ts'), 'utf-8');

// 使用自定义解析器解析代码
const ast = recast.parse(code, useBabelParser);

// 将AST转换回代码
const output = recast.print(ast).code;

//  将转换后的代码写入文件
fs.writeFileSync(path.resolve('output', 'recastSameOutput.ts'), output, 'utf-8');

// 比较原始代码和转换后的代码
if (code === output) {
  console.log('The transformed code is identical to the original code.');
} else {
  console.log('There are differences between the original and transformed code.');
}